﻿using System;
using System.ComponentModel.Composition;
using NetBay.Core.BusinessEntities.Workflow;
using Rio.Framework.Localisation;
using Rio.Framework.Pages.ChildWindows;
using Rio.Framework.Pages.Context;
using Rio.Applications.Web.Services;

namespace Rio.Framework.Pages.Workflow.Actions
{
    [InheritedExport(typeof(WorkflowButtonAction))]
    public class ReviveAllWorkflowButtonAction : WorkflowButtonAction
    {
        public ReviveAllWorkflowButtonAction()
        {
            Text = Resx.GetLabel(ResxWorkflow.WorkflowAction_Revive);
            Image = new Uri("/Rio.Framework.Pages;component/Assets/Images/revive.png", UriKind.Relative);
            UiTypeAction = WorkflowTypeAction.Revive;
            IsGlobalAction = true;
        }

        protected override void ActionClicked()
        {
            RemindWindow remindWindow = new RemindWindow(WorkflowInstance);
            remindWindow.Closed += remindWindow_Closed;
            remindWindow.ShowDialog();
        }

        private void remindWindow_Closed(object sender, Telerik.Windows.Controls.WindowClosedEventArgs e)
        {
            RemindWindow windows = (RemindWindow)sender;
            if (windows.DialogResult == true)
            {

                BusinessEntitiesDomainContext ds = GlobalWebContext.BusinessEntitiesDomainContext;
                ds.AddWorkflowHistoricalForAllDocuments(
                    WorkflowInstance.Identifier,
                    null,
                    GlobalWebContext.CurrentUser.UserId,
                    WorkflowState.Revived,
                    this.RulesAction.Identifier);

            }
            windows.Closed -= remindWindow_Closed;
        }
    }
}
